package edu.put.sk.nrpp.ui;

import java.util.ArrayList;

import edu.put.sk.nrpp.ConnectionList;
import edu.put.sk.nrpp.Floyd;

public class RouterList {
	public ArrayList<Router> routers=new ArrayList<Router>();
	public ArrayList<GraphicsRouter> graphicsRouters=new ArrayList<GraphicsRouter>();
	public ArrayList<Integer> allHop=new ArrayList<Integer>();
	public ConnectionList connectionList;
	public Integer programNumber = 0;
	private Floyd _floyd;
	//public int aasada;
	public RouterList(ConnectionList connectionList){
		this.connectionList=connectionList;
	}
	
	public void makeRouter(String name,String id,Integer areaNum, Integer membership1){
		Router router=new Router(connectionList, name, id, areaNum, membership1, programNumber++);
		GraphicsRouter gr = new GraphicsRouter(router);
		router.linkGR(gr);
		routers.add(router);
		graphicsRouters.add(gr);
		allHop.add(0);
	}
	
	public void deleteRouter(String id){
		for (int i=0;i<this.routers.size();i++){
			if (this.routers.get(i).id==id){
				this.routers.get(i).unlink();
				this.graphicsRouters.remove(this.routers.get(i).graphicsRouter);
				this.routers.remove(i);
				this.allHop.remove(i);
				break;
			}
		}
	}
	
	public void addDistance(String idA, int hopNumber){
		Router r;
		int iter=0;
		for (Object o:routers){			
			r = (Router)o;
			if (r.id.equals(idA)){
				allHop.set(iter, hopNumber);
				break;
			}
			iter++;
		}
	}
	
	public void connect(String idA, String idB){
		Router r1 = null;
		Router r2 = null;
		Router r;
		int i=0;
		for (Object o:this.routers){
			r = (Router)o;
			if (r.id.equals(idA)){
				r1=r;
				i++;
			}
			else if (r.id.equals(idB)){
				r2=r;
				i++;
			}
			if (i==2){
				break;
			}
		}
		if ((r1!=null)&&(r2!=null)){
			r1.link(r2);
		}
		else{
			System.out.println("Connect null point exception");
		}
	}
	
	public void disconnect(String idA, String idB){
		Router r1 = null;
		Router r2 = null;
		Router r;
		int i=0;
		for (Object o:this.routers){
			r = (Router)o;
			if (r.id.equals(idA)){
				r1=r;
				i++;
			}
			else if (r.id.equals(idB)){
				r2=r;
				i++;
			}
			if (i==2){
				break;
			}
		}
		if ((r1!=null)&&(r2!=null)){
			r1.deleteConnection(r2);
		}
		else{
			System.out.println("Connect null point exception");
		}
		
	}
	
	/*public Router minAllHop(){
		int min=2147483647;
		int position = -1;
		for (int i=0;i<allHop.size();i++){
			if (min>allHop.get(i)){
				min=allHop.get(i);
				position=i;
			}
		}
		if (position>-1){
			return (Router) routers.get(position);
		}
		else{
			return null;
		}
	}*/
	
	public ArrayList<Router> minAllHop(){
		int min = java.lang.Integer.MAX_VALUE;
		ArrayList<Router> minRouters = new ArrayList<Router>();
		boolean position = false;
		for (int i=0;i<allHop.size();i++){
			if (min>allHop.get(i)){
				min=allHop.get(i);
				minRouters.clear();
				minRouters.add(routers.get(i));
				position=true;
				System.out.println("Zmiana");
			}
			else if(min==allHop.get(i)){
				minRouters.add(routers.get(i));
			}
		}
		if (position==true){
			return minRouters;
		}
		
		return null;
	}
	
	public void show(){
		for (Object o:routers){
			Router r = (Router)o;
			System.out.println("Ruter : "+r.hostname);
			r.show();
		}
	}
	
	public void algorithmFloyd(){
		if (this._floyd==null){
			this._floyd = new Floyd(this);
		}
		this._floyd.run(this.routers);
	}
}
